    ;
    (function (window, angular) {
        'use strict';
        angular.module("Controller.BasicData.Depot", [])
            .controller("DepotCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, UserContextService) {
                $scope.handleBlur = function(e) {
                    $scope.condition.billNos = UserContextService.MultiInput(e.target.value);
                };
                $scope.activeFlag = [true, false, false, false, false, false];
                $scope.submitted = false;
                $scope.condition = {};
                $scope.depotList = [];
                $scope.depotPageInfo = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
                $scope.areaList = [];
                $scope.areaPageInfo = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
                $scope.placeList = [];
                $scope.placePageInfo = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
                $scope.markInfo = {
                    allChecked: false
                };
                $scope.printList = [];
                $scope.tempSet = {
                    qrCodeSize: 130,
                    showDept: true,
                    showArea: true,
                    showType: true,
                    showSeq: true
                };
                $scope.showPrint = false;
                $scope.toolbarConfig = {
                    showAdvanceSearch: false,
                    showColConfig: false
                };
                $scope.detailInfo = {};
                $scope.depotTabFlag = {};
                $scope.areaTabFlag = {};
                $scope.placeTabFlag = {};
                var currentDepot = {};
                var currentArea = {};
                var tabFlag = {
                    0: "新增",
                    1: "编辑"
                };

                var urlMap = {
                    depot: {
                        add: '/warehouse/warehouse',
                        del: '',
                        update: '/warehouse/update',
                        list: '/warehouse/listWarehouses'
                    },
                    area: {
                        add: '/warehouse/area',
                        del: '',
                        update: '/warehouse/updateArea',
                        list: '/warehouse/listAreas'
                    },
                    place: {
                        add: '/warehouse/locations',
                        del: '',
                        update: '/warehouse/updateLocationDesc',
                        list: '/warehouse/listLocations'
                    }
                };

                var btnPermissionCode = {
                    depotAdd: window.ActionCode.depotAdd,
                    depotAddArea: window.ActionCode.depotAddArea,
                    depotStopUsing: window.ActionCode.depotStopUsing,
                    areaStopUsing: window.ActionCode.areaStopUsing,
                    areaEdit: window.ActionCode.depotExport,
                    depotAddPlace: window.ActionCode.depotAddPlace,
                    depotPlacePrint: window.ActionCode.depotPlacePrint,
                    depotExport: window.ActionCode.depotExport,
                    depotDel: window.ActionCode.depotDel,
                };

                $scope.btnPermission = {
                    depotAdd: true,
                    depotAddArea: true,
                    depotAddPlace: true,
                    depotPlacePrint: true,
                    depotExport: true,
                    areaEdit: true,
                    depotDel: true,
                };

                function setBtnPermission() {
                    for (var key in btnPermissionCode) {
                        $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                    }
                }
                $scope.exportParam = {
                    allUrl: ybHttp.RequestUrl("device", "/warehouse/export")
                };
                $scope.changeTab = function (index) {
                    $scope.activeFlag = [false, false, false, false, false, false, false];
                    $scope.activeFlag[index] = true;
                    $scope.submitted = false;
                    switch (index) {
                        case 0:
                            initData();
                            break;
                        case 1:
                            break;
                        case 2:
                            $scope.placeTabFlag.areaNo = "";
                            $scope.condition.placeNo = "";
                            $scope.detailInfo = {};
                            break;
                        case 3:
                            break;
                        case 4:
                            $scope.detailInfo = {};
                            break;
                        case 5:
                            break;
                        case 6:
                            break;
                        default:
                            break;
                    }
                };

                function initData() {
                    $scope.tabFlag = {};
                    $scope.detailInfo = {};
                    $scope.areaTabFlag.depotNo = "";
                    $scope.placeTabFlag.areaNo = "";
                    $scope.condition.areaNo = "";
                }

                function getDataList(storageType, pNumber, curPage) {
                    var service = "device";
                    var reqUrl = urlMap[storageType].list;
                    var param = {};
                    param.pageSize = $scope[storageType + "PageInfo"].pageSize;
                    param.pageSize = $scope[storageType + "PageInfo"].pageSize;
                    param.keyWords = $scope.condition.keyWords || undefined;
                    param.billNos = $scope.condition.billNos || undefined;
                    param.purchaseOrgName = $scope.condition.purchaseOrgName || undefined;
                    param.warehouseType = $scope.condition.warehouseType || undefined;
                    // param.filters = angular.copy($scope.condition);
                    if (!!curPage) {
                        $scope[storageType + "PageInfo"].curPage = curPage;
                    }
                    param.pageNum = $scope[storageType + "PageInfo"].curPage;
                    if (storageType === "depot") {
                        param.orgId = "";
                    }
                    if (storageType === "area") {
                        param.warehouseId = pNumber;
                    }
                    if (storageType === "place") {
                        param.areaId = pNumber;
                    }
                    ybHttp.Get(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        angular.forEach(resp.value.list, function (item) {
                            item.isChecked = false;
                        });
                        if (storageType == "place") {
                            $scope.markInfo.allChecked = false;
                            angular.forEach(resp.value.list, function (item) {
                                item.isChecked = false;
                            })
                        }
                        $scope[storageType + "PageInfo"].totalRows = resp.value.total;
                        $scope[storageType + "PageInfo"].totalPages = resp.value.pages;
                        $scope[storageType + "List"] = resp.value.list;
                    });
                }

                $scope.getDataList = function (storageType, item, curPage) {
                    $scope[storageType + "List"] = [];
                    switch (storageType) {
                        case "area":
                            $scope.copyItem = item;
                            $scope.areaTabFlag.id = item.id;
                            $scope.areaTabFlag.depotNo = item.warehouseCode;
                            $scope.detailInfo.warehouseId = item.id;
                            $scope.detailInfo.warehouseCode = item.warehouseCode;
                            currentDepot = item;
                            $scope.changeTab(2);
                            break;
                        case "place":
                            $scope.placeTabFlag.id = item.id;
                            $scope.placeTabFlag.areaNo = item.areaCode;
                            $scope.detailInfo.warehouseAreaId = item.id;
                            currentArea = item;
                            $scope.changeTab(4);
                            break;
                        default:
                            break;
                    }
                    getDataList(storageType, item.id, curPage);
                };

                $scope.search = function (storageType, pNumber) {
                    getDataList(storageType, pNumber);
                };

                $scope.reset = function () {
                    $scope.condition = {};
                };

                $scope.switchToolbar = function (toolbar) {
                    if (!!$scope.toolbarConfig["show" + toolbar]) {
                        $scope.toolbarConfig["show" + toolbar] = false;
                    } else {
                        $timeout(function () {
                            $scope.toolbarConfig["show" + toolbar] = true;
                        });
                    }
                };
                $scope.removeDepot = function (depotId) {
                    ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                        var service = "device";
                        var reqUrl = "/warehouse/removeWareNoUsed?warehouseId=" + depotId;
                        ybHttp.Post(service, reqUrl).then(function (resp) {
                            if (resp.code !== 0) {
                                ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                                return;
                            }
                            ybWidget.ShowToaster('success', '操作成功');
                            getDataList("depot");
                        });
                    }, function () {

                    });
                };

                $scope.selectSingle = function (item) {
                    if (!item.isChecked) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        if (!checkAll()) {
                            $scope.markInfo.allChecked = false;
                        } else {
                            $scope.markInfo.allChecked = true;
                        }
                    }
                };

                $scope.selectAll = function () {
                    for (var i = 0, len = $scope.placeList.length; i < len; i++) {
                        $scope.placeList[i].isChecked = $scope.markInfo.allChecked;
                    }
                };

                function checkAll() {
                    var selectAll = false;
                    for (var i = 0, len = $scope.placeList.length; i < len; i++) {
                        if (!$scope.placeList[i].isChecked) {
                            selectAll = false;
                            break;
                        }
                        selectAll = true;
                    }
                    return selectAll;
                }

                function handlePrintDataList(item, data) {
                    var printObj = {};
                    printObj.locationCode = item.locationCode;
                    printObj.columnNo = item.columnNo;
                    printObj.floorNo = item.floorNo;
                    printObj.rowNo = item.rowNo;
                    printObj.seqNo = item.seqNo;
                    printObj.depot = $scope.areaTabFlag.depotNo;
                    printObj.area = $scope.placeTabFlag.areaNo;
                    printObj.shelfType = item.shelfType;
                    printObj.shelfNo = item.shelfNo;
                    data.push(printObj);
                }

                $scope.batchPrint = function () {
                    $scope.showPrint = true;
                    $scope.printList = [];
                    angular.forEach($scope.placeList, function (item) {
                        if (item.isChecked) {
                            handlePrintDataList(item, $scope.printList);
                        }
                    });
                    if ($scope.printList.length === 0) {
                        ybWidget.ShowMessage('请选择要打印的储位');
                        return;
                    }
                    setTimeout(function () {
                        printFunc()
                    }, 300);
                };
                $scope.print = function (item) {
                    $scope.showPrint = true;
                    $scope.printList = [];
                    handlePrintDataList(item, $scope.printList);

                    $timeout(function () {
                        printFunc()
                    }, 300);
                };

                async function printFunc() {
                    var printTable = document.getElementById('printStorage');
                    var lodop = UserContextService.GetLodop();
                    if (!lodop) {
                        return;
                    }

                    if (!await UserContextService.InitPrinter(lodop, true, 0)) {
                        return;
                    }

                    lodop.ADD_PRINT_HTM(0, 0, '90mm', '60mm', printTable.innerHTML);
                    lodop.PREVIEW();
                    $scope.showPrint = false;
                    $scope.$apply();
                }

                $scope.stopUsingState = function (type,item,storageType){
                    var service = "device";
                    var reqUrl = "/warehouse/area/stopOrOpen";
                    if(type == "stop"){
                        var param ={
                            areaId:item.id,
                            stopUsing:1,
                            storageType: storageType
                        }
                    }
                    if(type == "use"){
                        var param ={
                            areaId:item.id,
                            stopUsing:0,
                            storageType: storageType
                        }
                    }
                    $scope.copyItem = item;
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        if('area' === storageType) {
                            $scope.getDataList('area', $scope.copyItem, 1);
                        } else {
                            $scope.getDataList('depot', $scope.copyItem, 1);
                        }
                    });
                }

                $scope.addData = function (storageType, item) {
                    switch (storageType) {
                        case "depot":
                            $scope.depotTabFlag.type = 0;
                            $scope.depotTabFlag.name = tabFlag[0];
                            $scope.detailInfo.isPlan = 0;
                            $scope.detailInfo.isCenter = 0;
                            $scope.detailInfo.firstInOut = 0;
                            $scope.detailInfo.isBarCode = 1;
                            $scope.changeTab(1);
                            break;
                        case "area":
                            $scope.areaTabFlag.type = 0;
                            $scope.areaTabFlag.name = tabFlag[0];
                            $scope.detailInfo.warehouseId = angular.isDefined(item) ? item.id : currentDepot.id;
                            $scope.detailInfo.warehouseNo = angular.isDefined(item) ? item.warehouseCode : currentDepot.warehouseCode;
                            $scope.changeTab(3);
                            break;
                        case "place":
                            $scope.placeTabFlag.type = 0;
                            $scope.placeTabFlag.name = tabFlag[0];
                            $scope.detailInfo.warehouseCode = currentDepot.warehouseCode;
                            $scope.detailInfo.warehouseAreaId = angular.isDefined(item) ? item.id : currentArea.id;
                            $scope.detailInfo.areaNo = angular.isDefined(item) ? item.areaCode : currentArea.areaCode;
                            $scope.changeTab(5);
                            break;
                        default:
                            break;
                    }
                };

                $scope.editDepot = function (item) {
                    $scope.depotTabFlag.type = 1;
                    $scope.depotTabFlag.name = tabFlag[1];
                    $scope.changeTab(1);
                    $scope.detailInfo = JSON.parse(JSON.stringify(item));
                };

                $scope.updateData = function (item) {
                    $scope.placeTabFlag.type = 1;
                    $scope.detailInfo = item;
                    $scope.changeTab(6);
                };

                // 删除货架
                $scope.deleteShelf = function () {
                    var setting = {
                        warehouseCode: $scope.areaTabFlag.depotNo,
                        warehouseAreaId: currentArea.id,
                        areaNo: currentArea.areaCode
                    };
                    ybWidget.DeleteShelf(setting).then(function (data) {
                        if (data === 'success') {
                            $scope.search('place', $scope.placeTabFlag.id);
                        }
                    });
                };

                // 修改库区
                $scope.editArea = function (data) {
                  $scope.areaTabFlag.type = 1;
                  $scope.areaTabFlag.name = tabFlag[1];
                  $scope.detailInfo.id = data.id;
                  $scope.detailInfo.warehouseNo = currentDepot.warehouseCode;
                  $scope.detailInfo.areaCode = data.areaCode;
                  $scope.detailInfo.description = data.description;
                  $scope.changeTab(3);
                }

                function changeTab(index, flag, storageType) {
                    if (!!flag) {
                        if (flag == "editPlace") {
                            $scope.activeFlag[index - 2] = true;
                        } else {
                            $scope.activeFlag[index - 1] = true;
                        }
                        if (!!storageType) {
                            getDataList(storageType, $scope[storageType + "TabFlag"].id);
                        }
                    } else {
                        $scope.activeFlag[index - 3] = true;
                    }
                    $scope.activeFlag[index] = false;
                }

                $scope.cancel = function (index, flag) {
                    changeTab(index, flag);
                    $scope.detailInfo = {};
                };

                $scope.saveData = function (isValid, storageType, index, flag) {
                    if (!isValid) {
                        $scope.submitted = true;
                        return;
                    }
                    if (storageType == 'place') {
                        // 储位的保存
                        var placeNum = ($scope.detailInfo.endRowNo - $scope.detailInfo.beginRowNo + 1) * $scope.detailInfo.floorNum * $scope.detailInfo.columnNum * $scope.detailInfo.locationNum;
                        if (placeNum > 200000) {
                            ybWidget.ShowMessage('储位数量不能超过20万', MESG_TYPE.Error);
                            return;
                        }
                    }
                    var service = "device";
                    var reqUrl = urlMap[storageType].add;
                    if ($scope[storageType + "TabFlag"].type === 1) {
                        reqUrl = urlMap[storageType].update;
                    }
                    var param = angular.copy($scope.detailInfo);

                    // 库区编辑
                    if (storageType == 'area' && $scope.areaTabFlag.type == 1) {
                      param = {
                        id: $scope.detailInfo.id,
                        description: $scope.detailInfo.description
                      }
                    }

                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        changeTab(index, flag, storageType);
                        $scope.detailInfo = {};
                    });
                };

                $scope.chuhuodiList = [];
                function getChuhuodis() {
                    var service = "device";
                    var reqUrl = "/materials/suplierFactoryIds";
                    ybHttp.Get(service, reqUrl).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        $scope.chuhuodiList = resp.value.map(vlm => ({ id: vlm, name: vlm }));
                    });
                }

                (function () {
                    getDataList("depot");
                    UserContextService.SetBtnPermission(setBtnPermission);
                    getChuhuodis();
                })();
            }]);
    })(window, window.angular);
